package cn.com.ddapp.ddm.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zhy.autolayout.utils.AutoUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.com.ddapp.ddm.AppConfig;
import cn.com.ddapp.ddm.R;
import cn.com.ddapp.ddm.api.OKUtilAPI;
import cn.com.ddapp.ddm.bean.CircleBean;
import cn.com.ddapp.ddm.ui.ShareImgActivity;
import cn.com.ddapp.ddm.util.TextUtil;
import cn.com.ddapp.ddm.widget.CircleImageView;
import cn.com.ddapp.ddm.widget.GridViewForListView;

/**
 * Created by Administrator on 2016/6/28.
 */
   public class ShareListAdapter extends BaseAdapter{

    private Context context;
    private List<CircleBean.CircleList> shareList;


    ShareItemImageAdapter imgAdapter;
    public ShareListAdapter(Context context, List<CircleBean.CircleList> shareList){
        this.context=context;
        this.shareList=shareList;
    }

    @Override
    public int getCount() {
        return shareList != null ? shareList.size():0;
    }

    @Override
    public Object getItem(int i) {
        return shareList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    public void addView(List<CircleBean.CircleList> list){
        if(list == null || list.size()<=0){
            return;
        }
        shareList.addAll(list);
    }

//    public void clear(){
//        shareList.clear();
//    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if(view == null){
            view= LayoutInflater.from(context).inflate(R.layout.sharelist_item,null);
            viewHolder=new ViewHolder(view);
            view.setTag(viewHolder);
            AutoUtils.auto(view);
        }else{
            viewHolder= (ViewHolder) view.getTag();
        }

        try {

            String name = URLDecoder.decode(shareList.get(i).getNickName(), "utf-8");
            viewHolder.share_userName.setText(name);
            viewHolder.share_content.setText(URLDecoder.decode(shareList.get(i).getTitle(), "utf-8"));
            viewHolder.share_time.setText(shareList.get(i).getCreateTimeStr());
            if(TextUtils.isEmpty(shareList.get(i).getAddress())){
                viewHolder.share_addimg.setVisibility(View.GONE);
            }else if(!TextUtils.isEmpty(shareList.get(i).getAddress())){
                viewHolder.share_addimg.setVisibility(View.VISIBLE);
            }
            if(TextUtils.isEmpty(shareList.get(i).getNickName())){
                viewHolder.share_vip.setVisibility(View.GONE);
            }else if(!TextUtils.isEmpty(shareList.get(i).getNickName())){
                viewHolder.share_vip.setVisibility(View.VISIBLE);
            }
            viewHolder.share_address.setText(shareList.get(i).getAddress());

            if(!TextUtils.isEmpty(shareList.get(i).getHeadImg())){
                if(shareList.get(i).getHeadImg().contains("http")){
                    Glide.with(context).load(shareList.get(i).getHeadImg()).into(viewHolder.share_headImg);
                }else {
                    Glide.with(context).load(OKUtilAPI.imgBaseUrl + shareList.get(i).getHeadImg()).into(viewHolder.share_headImg);
                }
            }else{
                Glide.with(context).load("http://img.taopic.com/uploads/allimg/120421/107063-12042114025737.jpg").into(viewHolder.share_headImg);
            }
            String imgs=shareList.get(i).getImgs();

            final   List<String> imgList=new ArrayList<String>();
            final ArrayList<String> arrayList = new ArrayList<String>();
            if(!TextUtils.isEmpty(imgs)){
                String getimgs[]=imgs.split(",");
//            Log.i("aaa","--a--"+imgs);
                for(int a=0;a<getimgs.length;a++){
                    imgList.add(getimgs[a]);
                    arrayList.add(getimgs[a]);

                }
                viewHolder.share_imglist.setVisibility(View.VISIBLE);
                imgAdapter=new ShareItemImageAdapter(context,imgList );
                viewHolder.share_imglist.setAdapter(imgAdapter);
                viewHolder.share_imglist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                        ShareImgActivity.startIntent(context,arrayList,i);
                        AppConfig.TOMAIN="3";
                    }
                });
            }else{
                viewHolder.share_imglist.setVisibility(View.GONE);
//                imgAdapter=new ShareItemImageAdapter(context,imgList );
//                viewHolder.share_imglist.setAdapter(imgAdapter);
//                viewHolder.share_imglist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//                    @Override
//                    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//                        ShareImgActivity.startIntent(context,arrayList);
//                    }
//                });
            }

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }


        return view;
    }

    public static class ViewHolder{
        @BindView(R.id.share_headImg)
        CircleImageView share_headImg;
        @BindView(R.id.share_userName)
        TextView share_userName;
        @BindView(R.id.share_vip)
        ImageView share_vip;
        @BindView(R.id.share_content)
        TextView share_content;
        @BindView(R.id.share_imglist)
        GridViewForListView share_imglist;
        @BindView(R.id.share_time)
        TextView share_time;
        @BindView(R.id.share_address)
        TextView share_address;
        @BindView(R.id.share_addimg)
        ImageView share_addimg;
        public ViewHolder(View view){
            ButterKnife.bind(this,view);
        }
    }
}
